﻿@page "/"
@inject NotificationService Notification

<h1>SignalR Broadcast</h1>

<ul>
    <li>
        <a href="/" target="new">Open another window</a>
    </li>
    <li>
        Click Connect
    </li>
    <li>
        Enter Name and Broadcast Message. Both screens will receive the broadcasts. 
    </li>
</ul>

<div class="alert alert-warning">
    @Log
</div>

<div class="alert alert-info">
    @Message
</div>

<button onclick="@ConnectAsync">Connect</button>
<br/><br/>
<label>Username</label>
<input class="form-control" type="text" bind="@Sender" placeholder="Enter username" />
<br/>
<label>Broadcast Message</label>
<textarea class="form-control" bind="@BroadcastMessage" placeholder="Enter Broadcast Message"></textarea>
<button onclick="@BroadcastAsync">Broadcast</button>

@functions
{
    string Message { get;set;}
    string Log { get;set;}

    string Sender { get;set;}
    string BroadcastMessage { get;set;}

    protected override void OnInit()
    {
        Notification.OnMessage = (string user, string message) =>
        {
            Message = $"{user} broadcasts \"{message}\"";
            this.Invoke(StateHasChanged);
        };
    }

    async Task ConnectAsync()
    {
        Log = "Connect";
        await Notification.ConnectAsync();
    }

    async Task BroadcastAsync()
    {
        Log = "Broadcast";
        await Notification.BroadcastAsync(Sender, BroadcastMessage);
    }
}
